using System;
using System.Security.Cryptography;
using System.Text;

namespace PT.Common
{
    public class DESEncrypt
    {
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static string Encrypt(string plainText)
        {
            return Encrypt(plainText, "12-45%78");//必须8位
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public static string Decrypt(string cipherText)
        {
            return Decrypt(cipherText, "12-45%78");//必须8位
        }

        public static string Encrypt(string plainText, string key)
        {
            try
            {
                // 确保密钥为8位
                byte[] rgbKey = Encoding.UTF8.GetBytes(key.Length >= 8 ? key.Substring(0, 8) : key.PadRight(8, '0'));
                byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; // 默认向量[1,4](@ref)

                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Mode = CipherMode.CBC; // 推荐使用CBC模式[2](@ref)
                    des.Padding = PaddingMode.PKCS7; // 兼容性更好的填充方式[2,5](@ref)

                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                        {
                            byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
                            cs.Write(inputBytes, 0, inputBytes.Length);
                            cs.FlushFinalBlock();
                        }
                        return Convert.ToBase64String(ms.ToArray()); // 返回Base64编码结果[1,4](@ref)
                    }
                }
            }
            catch
            {
                return plainText; // 失败时返回原文[1](@ref)
            }
        }



        public static string Decrypt(string cipherText, string key)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(key.Length >= 8 ? key.Substring(0, 8) : key.PadRight(8, '0'));
                byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Mode = CipherMode.CBC;
                    des.Padding = PaddingMode.PKCS7;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                        {
                            byte[] inputBytes = Convert.FromBase64String(cipherText);
                            cs.Write(inputBytes, 0, inputBytes.Length);
                            cs.FlushFinalBlock();
                        }
                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
            catch
            {
                return cipherText; // 失败时返回密文[1,4](@ref)
            }
        }


        public static void Demo()
        {
            string original = "Hello,,,,,, DES!";

            string encrypted = Encrypt(original);
            Console.WriteLine($"加密结果: {encrypted}");

            string decrypted = Decrypt(encrypted);
            Console.WriteLine($"解密结果: {decrypted}");
        }


    }
}